﻿using SQLiteDB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using Utils.Common;
using ZK.Entity;

namespace ZK.BLL
{
  public  class PrintDataBLL
    {

        private PrintDataBLL() { }
        /// <summary>
        /// 窗体对象实例
        /// </summary>
        private static PrintDataBLL _instance;
        public static PrintDataBLL Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new PrintDataBLL();
                return _instance;
            }
        }


        public string GetNewID()
        {
            return BaseIdGenerator.Instance.GetId();
        }

        //public PrintData GetFirst()
        //{
        //    string sql = "Select * from PrintData Where IsPrinted=0 Order BY Id ";
        //    return SQLiteORM.Instance.GetEntity<PrintData>(sql);
        //}

        public PrintData Get (string id)
        {
            string sql = "Select * from PrintData Where  Id='"+ id + "' ";
            return SQLiteORM.Instance.GetEntity<PrintData>(sql);
        }

        //public PrintData GetFirst(string matCode,string  so)
        //{
        //    string sql = "Select * from PrintData Where MatCode='"+matCode+ "' AND SO='"+ so + "'  AND  IsPrinted=0 Order BY Id ";
        //    return SQLiteORM.Instance.GetEntity<PrintData>(sql);
        //}


        //public PrintData GetRepeate(string sno)
        //{
        //    string sql = "Select * from PrintData Where  SNo='"+ sno + "'";
        //    return SQLiteORM.Instance.GetEntity<PrintData>(sql );
        //}


        public List<PrintData> GetPageList( string customerName, string productName,string modelNo , string batchNo,  int pageIndex, int pageSize, out int total, out int pageCount)
        {
            string sql = "  1=1 ";
            if (!string.IsNullOrEmpty(customerName))
                sql += "AND CustomerName like '%" + customerName + "%' ";
            if (!string.IsNullOrEmpty(productName))
                sql += "AND ProductName like '%" + productName + "%' ";
            if (!string.IsNullOrEmpty(modelNo))
                sql += "AND ModelNo like '%" + modelNo + "%' ";
            if (!string.IsNullOrEmpty(batchNo))
                sql += "AND BatchNo like '%" + batchNo + "%' ";
 
          var list = SQLiteORM.Instance.GetPageList<PrintData>("*", "PrintData", sql, "Id DESC", pageIndex, pageSize, out total, out pageCount);
            return list==null?new List<PrintData>():list.ToList();
        }



        public List<PrintData> GetList(  string matCode, string productName, string sNo, string so )
        {
            string sql = " Select * from  PrintData Where 1=1 AND IsPrinted =1 ";
 
            if (!string.IsNullOrEmpty(matCode))
                sql += "AND MatCode like '%" + matCode + "%' ";
            if (!string.IsNullOrEmpty(productName))
                sql += "AND ProductName like '%" + productName + "%' ";
            if (!string.IsNullOrEmpty(sNo))
                sql += "AND SNo like '%" + sNo + "%' ";
            if (!string.IsNullOrEmpty(so))
                sql += "AND SO like '%" + so + "%' ";

            sql += "Order BY Id";
           var list = SQLiteORM.Instance.GetEntityList<PrintData>(sql);

            return list != null ? list : new List<PrintData>();
        }


        public bool Delete(PrintData printData)
        {
            return SQLiteORM.Instance.Delete(printData, nameof(PrintData.Id));
        }


        public bool Delete( string id)
        {
            string sql = " Delete from  PrintData Where   Id ='"+id+"'";
            return SQLiteORM.Instance.Delete(sql);
        }


        public bool InsertList(List<PrintData> list)
        {
            return SQLiteORM.Instance.InsertListByTransaction<PrintData>(list);
        }

        public bool Update(PrintData printData)
        {
            return SQLiteORM.Instance.Update(printData,nameof(PrintData.Id));
        }

        public bool Insert ( PrintData  printData)
        {
            return SQLiteORM.Instance.Insert (printData);
        }

    }
}
